package com.liang.gulimall.order.listener;

import com.liang.common.to.mq.SeckillOrderTo;
import com.liang.gulimall.order.entity.OrderEntity;
import com.liang.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
@Slf4j
@Service
@RabbitListener(queues = "order.seckill.order.queue")
public class OrderSeckillListener {
    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void orderCloseListener(SeckillOrderTo seckillOrderTo, Message message, Channel channel) throws IOException {
        log.info("秒杀订单");
        try {
            orderService.createSeckillOrder(seckillOrderTo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            log.error("订单关闭异常");
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }

}
